﻿using CRM.Models;
using System.Threading.Tasks;
using System;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using Microsoft.AspNetCore.Identity;

namespace CRM.Services
{
    public class OperationLogService : BaseService
    {
        private readonly MyDbContext _dbContext;
        private readonly HttpContext _httpContext;
        private readonly UserManager<User> _userManager;

        public OperationLogService(MyDbContext dbContext, HttpContext httpContext, UserManager<User> userManager)
        {
            _dbContext = dbContext;
            this._httpContext = httpContext;
            this._userManager = userManager;
        }

        public async Task<bool> AddLogAsync(List<CustomerOperationLog> logs)
        {
            var user = await _userManager.GetUserAsync(_httpContext.User);
            var Time = DateTime.Now;
            foreach (var log in logs)
            {
                log.UpdatedAt = Time;
                log.CreatedAt = Time;
                log.OemId = user.OemId;
                log.CompanyId = user.CompanyId;
                log.GroupId = user.GroupId;
                log.UserId = user.Id;

                _dbContext.CustomerOperationLog.Add(log);
            }
            var status = _dbContext.SaveChanges();
            if (status == 1)
            {
                return true;

            }
            return false;
        }

        public async Task<bool> AddLogAsync(CustomerOperationLog log)
        {
            var user = await _userManager.GetUserAsync(_httpContext.User);

            var Time = DateTime.Now;
            log.UpdatedAt = Time;
            log.CreatedAt = Time;
            log.OemId = user.OemId;
            log.CompanyId = user.CompanyId;
            log.UserId = user.Id;
            _dbContext.CustomerOperationLog.Add(log);
            var status = _dbContext.SaveChanges();
            if (status == 1)
            {
                return true;

            }
            return false;
        }
    }
}
